package com.hns.phone.maintenance.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.frame.mybatis.Pagination;
import com.hns.phone.common.vo.PhoneParamsVo;
import com.hns.phone.entity.AppCarMaintenInfoEntity;
import com.hns.phone.maintenance.service.PhoneCarMaintService;
import com.hns.phone.maintenance.vo.ExaminReportDetailVo;
import com.hns.phone.maintenance.vo.MaintenanceParamVo;
import com.hns.tool.pub.PubUtil;

@Controller
@RequestMapping("/api/phoneCarMaint")
public class PhoneCarMaintController extends BaseController{
	
	@Autowired
    private PhoneCarMaintService phoneCarMaintePlanService;
	
	@Autowired
    protected CommonInitParamService commonInitParamService;
	
	/**
     * 查询维保过期
     * @param conditionVo
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/getMaintenancePlanPage")
    @ResponseBody
    public Object getMaintenancePlanPage(MaintenanceParamVo conditionVo) throws Exception {
    	SysUserEntity sysUserEntity=this.getPhoneCurrentUser();
    	conditionVo.setCorpId(sysUserEntity.getCorpId());
        Pagination page = phoneCarMaintePlanService.searchMaintenancePage(conditionVo);
        GridPage grid = new GridPage(page);
        return getReturnResult(grid);
    }
    
    @RequestMapping("/getSoloMaintenanceInfo")
    @ResponseBody
    public Object getSoloMaintenanceInfo(MaintenanceParamVo conditionVo) throws Exception{
    	SysUserEntity sysUserEntity=this.getPhoneCurrentUser();
    	conditionVo.setCorpId(sysUserEntity.getCorpId());
    	List<AppCarMaintenInfoEntity> list = phoneCarMaintePlanService.getSoloMaintenanceInfo(conditionVo);
    	AppCarMaintenInfoEntity entity = null;
    	if(PubUtil.isNotEmpty(list)){
    		entity = list.get(0);
    	}
    	return getReturnResult(entity);
    }
    
    /**
     * 获取维保记录
     * @param conditionVo
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getMaintenanceRecord")
    @ResponseBody
    public Object getMaintenanceRecord(MaintenanceParamVo vo){
    	//commonInitParamService.dateType2DateTime(vo, vo.getDateType());
    	Pagination page = phoneCarMaintePlanService.getMaintenanceRecord(vo);
    	GridPage grid = new GridPage(page);
    	return this.getReturnResult(grid);
    }
    
    @RequestMapping("/getLastExaminReport")
    @ResponseBody
    public Object getLastExaminReport() throws Exception {
    	Pagination page = phoneCarMaintePlanService.getLastExaminReport(this.getPhoneCurrentUser());
    	GridPage grid = new GridPage(page);
        return getReturnResult(grid);
    }
    
    @RequestMapping("/getExaminReportDetail")
    @ResponseBody
    public Object getExaminReportDetail(PhoneParamsVo vo) throws Exception{
    	vo.setCorpId(this.getPhoneCurrentUser().getCorpId());
    	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    	Calendar c = Calendar.getInstance();
        c.setTime(sf.parse(vo.getExamTime()));   //设置当前日期  
        c.add(Calendar.DATE, -1); //日期减1天,获取前一天数据
        vo.setExamTime(sf.format(c.getTime()));
//    	vo.setExamTime(getLastExamTime());
//    	vo.setExamTime("2016-01-31");
    	List<ExaminReportDetailVo> list = phoneCarMaintePlanService.getExaminReportDetail(vo);
    	return this.getReturnResult(list);
    }
    
    
    /**
     * 查询维保计划
     * @param conditionVo
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/getMaintenancePlanList")
    @ResponseBody
    public Object getMaintenancePlanList(MaintenanceParamVo conditionVo) throws Exception {
    	conditionVo.setCorpId(this.getPhoneCurrentUser().getCorpId());
        List list= phoneCarMaintePlanService.getMaintenancePlanList(conditionVo);
        return getReturnResult(list);
    }
    
    
    /**
     * 获取昨天的体检时间
     * @param exam_time
     * @return
     * @throws ParseException
     * @author:yecb
     * @email:lps00@163.com
     */
    private String getLastExamTime() throws ParseException{
    	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());   //设置当前日期  
        c.add(Calendar.DATE, -1); //日期减1天,获取前一天数据
    	return sf.format(c.getTime());
    }
    
}
